package com.xxg.renrenproduct.controller;

import com.xxg.renrencommon.domain.bean.CarryProductSettingInfo;
import com.xxg.renrencommon.domain.result.BaseResult;
import com.xxg.renrenproduct.domain.query.*;
import com.xxg.renrenproduct.service.CarryProductSettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 带货设置管理
 *
 * @author szj
 * @date 2021/11/29
 */
@Api(tags = "带货设置管理")
@RestController
@RequestMapping("/carryProductSetting")
@AllArgsConstructor
public class CarryProductSettingController {
    private final CarryProductSettingService carryProductSettingService;

    @ApiOperation("设置全局带货权限")
    @RequestMapping(value = "setGlobal", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "query", value = "设置全局带货权限参数", required = true, dataType = "SetGlobalQuery")})
    public BaseResult<Void> setGlobal(@RequestBody @Validated SetGlobalQuery query, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return carryProductSettingService.setGlobal(userId, query);
    }

    @ApiOperation("设置单个带货权限")
    @RequestMapping(value = "setSingle", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "query", value = "设置单个带货权限参数", required = true, dataType = "SetSingleQuery")})
    public BaseResult<Void> setSingle(@RequestBody @Validated SetSingleQuery query, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return carryProductSettingService.setSingle(userId, query);
    }

    @ApiOperation("设置全局带货系数")
    @RequestMapping(value = "setGlobalRatio", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "query", value = "设置全局带货系数参数", required = true, dataType = "SetGlobalRatioQuery")})
    public BaseResult<Void> setGlobalRatio(@RequestBody @Validated SetGlobalRatioQuery query, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return carryProductSettingService.setGlobalRatio(userId, query);
    }

    @ApiOperation("设置单个带货系数")
    @RequestMapping(value = "setSingleRatio", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "query", value = "设置单个带货系数参数", required = true, dataType = "SetSingleRatioQuery")})
    public BaseResult<Void> setSingleRatio(@RequestBody @Validated SetSingleRatioQuery query, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return carryProductSettingService.setSingleRatio(userId, query);
    }

    @ApiOperation("查询全局设置")
    @RequestMapping(value = "getGlobalSetting", method = RequestMethod.GET)
    public BaseResult<CarryProductSettingInfo> getGlobalSetting(HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return carryProductSettingService.getGlobalSetting(userId);
    }

    @ApiOperation("查询单个设置")
    @RequestMapping(value = "getSpuSetting", method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(name = "spuId", value = "spuId", required = true, dataType = "Integer", paramType = "query")})
    public BaseResult<CarryProductSettingInfo> getSpuSetting(@RequestParam("spuId") Integer spuId, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return carryProductSettingService.getSpuSetting(userId, spuId);
    }

    @ApiOperation("查询单个设置(包含非自己)")
    @RequestMapping(value = "getSpuSettingContainOther", method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(name = "spuId", value = "spuId", required = true, dataType = "Integer", paramType = "query")})
    public BaseResult<CarryProductSettingInfo> getSpuSettingContainOther(@RequestParam("spuId") Integer spuId, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return carryProductSettingService.getSpuSettingContainOther(userId, spuId);
    }

    @ApiOperation("查询是否存在单个设置")
    @RequestMapping(value = "hasSpuSetting", method = RequestMethod.GET)
    public BaseResult<Boolean> hasSpuSetting(HttpServletRequest request) {
        String userId = request.getHeader("userId");
        return carryProductSettingService.hasSpuSetting(userId);
    }
}
